package com.ly.honor.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.honor.entity.dto.NewsCreateDTO;
import com.ly.honor.entity.dto.NewsEditDTO;
import com.ly.honor.entity.dto.NewsQueryDTO;
import com.ly.honor.entity.vo.NewsQueryVO;
import com.ly.honor.service.NewsService;
import com.ly.honor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * (News)表控制层
 *
 * @author makejava
 * @since 2022-11-16 11:53:59
 */
@RestController
@RequestMapping("news")
@Validated
@Api(tags = "新闻管理")
public class NewsController  {
    /**
     * 服务对象
     */
    @Resource
    private NewsService newsService;

    /**
     * 分页查询所有数据
     *
     * @param pageNum 当前页
     * @param pageSize 页大小
     * @param newsDTO 查询实体
     * @return 所有数据
     */
    @ApiOperation("条件分页查询")
    @PostMapping("getNewsPageList/{pageNum}/{pageSize}")
    public Result<Page<NewsQueryVO>> getNewsPageList(@PathVariable("pageNum") long pageNum,
                            @PathVariable("pageSize") long pageSize,
                            @RequestBody(required = false) NewsQueryDTO newsDTO) {
        Page<NewsQueryVO> newsQueryVOPage =  newsService.getNewsPageList(pageNum,pageSize,newsDTO);
        return Result.ok(newsQueryVOPage);
    }

    @ApiOperation("条件分页查询按时间降序")
    @GetMapping("getNewsPageListDesc/{pageNum}/{pageSize}")
    public Result<Page<NewsQueryVO>> getNewsPageListDesc(@PathVariable("pageNum") long pageNum,
                                                     @PathVariable("pageSize") long pageSize) {
        Page<NewsQueryVO> newsQueryVOPage =  newsService.getNewsPageListDesc(pageNum,pageSize);
        return Result.ok(newsQueryVOPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @GetMapping("getNewsById/{id}")
    public Result<NewsQueryVO> getNewsById(@PathVariable long id) {
        NewsQueryVO newsQueryVO = newsService.getNewsById(id);
        return Result.ok(newsQueryVO);
    }

    /**
     * 新增数据
     *
     * @param newsCreateDTO 实体对象
     * @return 新增结果
     */
    @ApiOperation("新增新闻")
    @PostMapping("insertNews")
    public Result insertNews(@RequestBody(required = true)
                                 @Valid NewsCreateDTO newsCreateDTO) {
        newsService.insertNews(newsCreateDTO);
        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param newsEditDTO 实体对象
     * @return 修改结果
     */
    @ApiOperation("修改新闻")
    @PostMapping("updateNews")
    public Result update(@RequestBody @Valid NewsEditDTO newsEditDTO) {
        newsService.updateNewsById(newsEditDTO);
        return Result.ok();
    }

    /**
     * 通过id删除新闻
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("deleteNewsById/{id}")
    @ApiOperation("通过id删除新闻")
    public Result delete(@PathVariable("id") Long id) {
        newsService.removeById(id);
        return Result.ok();
    }

    /**
     * 批量删除新闻
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("removeBatch")
    @ApiOperation("批量删除新闻")
    public Result removeBatch(@RequestBody List<Long> idList) {
        newsService.removeByIds(idList);
        return Result.ok();
    }

    @PostMapping("upload")
    @ApiOperation("文件上传")
    public Result uploadFile(MultipartFile file){
        String url = newsService.upload(file);
        return Result.ok(url).message("上传文件成功");
    }

    @GetMapping("recentlyHighHeatNews")
    @ApiOperation("最近一个月热度前三的新闻")
    public Result recentlyHighHeatNews(){
        List<NewsQueryVO> newsQueryVOList = newsService.recentlyHighHeatNews();
        return Result.ok(newsQueryVOList);
    }


    @GetMapping("newsCount")
    @ApiOperation("查询有多少条新闻")
    public Result newsCount(){
        Integer newsCount = newsService.count();
        return Result.ok(newsCount);
    }

    @GetMapping("newsHeatRaise")
    @ApiOperation("新闻热度+1")
    public Result newsHeatRaise(Integer id){
        newsService.newsHeatRaise(id);
        return Result.ok();
    }

    @PostMapping("uploadVideo")
    @ApiOperation("上传视频")
    public Result uploadVideo(MultipartFile file){
        String url = newsService.uploadVideo(file);
        return Result.ok(url).message("上传视频成功");
    }
}

